package org.madgecko.api.uitl.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by MadFrog on 2017/7/24.
 */
public class StringToDateConverterFactory implements ConverterFactory<String, Date> {
    @Override
    public Converter getConverter(Class aClass) {
        return new StringToDate();
    }

    private class StringToDate implements Converter<String, Date>{

        public StringToDate() {
        }

        @Override
        public Date convert(String s) {
            try {
                if(s.length()==10){
                    s += " 23:59:59";
                }
                if(s.length()==16){
                    s += ":00";
                }
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date d = df.parse(s);
                return d;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
